package com.atguigu.gulimall.coupon.service.impl;

import com.atguigu.common.to.MemberPrice;
import com.atguigu.common.to.SkuReductionTo;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.coupon.dao.SkuFullReductionDao;
import com.atguigu.gulimall.coupon.entity.MemberPriceEntity;
import com.atguigu.gulimall.coupon.entity.SkuFullReductionEntity;
import com.atguigu.gulimall.coupon.entity.SkuLadderEntity;
import com.atguigu.gulimall.coupon.service.MemberPriceService;
import com.atguigu.gulimall.coupon.service.SkuFullReductionService;
import com.atguigu.gulimall.coupon.service.SkuLadderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    private SkuLadderService skuLadderService;

    private MemberPriceService memberPriceService;

    @Autowired
    public SkuFullReductionServiceImpl(SkuLadderService skuLadderService,
                                       MemberPriceService memberPriceService) {
        this.skuLadderService = skuLadderService;
        this.memberPriceService = memberPriceService;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReductions(List<SkuReductionTo> skuReductionTos) {
        List<SkuLadderEntity> skuLadderEntities = new ArrayList<>();
        List<SkuFullReductionEntity> skuFullReductionEntities = new ArrayList<>();
        List<MemberPriceEntity> memberPriceEntityList = new ArrayList<>();
        for (SkuReductionTo skuReductionTo : skuReductionTos) {
            // sku 阶梯价格
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            skuLadderEntity.setSkuId(skuReductionTo.getSkuId());
            skuLadderEntity.setDiscount(skuReductionTo.getDiscount());
            skuLadderEntity.setFullCount(skuReductionTo.getFullCount());
            skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
            // 过滤掉折扣数量价格不大于0的
            if (skuLadderEntity.getFullCount().compareTo(0) > 0) {
                skuLadderEntities.add(skuLadderEntity);
            }
            // sku 满减
            SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
            BeanUtils.copyProperties(skuReductionTo, skuFullReductionEntity);
            skuFullReductionEntity.setAddOther(skuReductionTo.getPriceStatus());
            // 过滤掉满减价格为不大于0的
            if (skuFullReductionEntity.getFullPrice().compareTo(BigDecimal.ZERO) > 0) {
                skuFullReductionEntities.add(skuFullReductionEntity);
            }
            // 会员价格
            List<MemberPrice> memberPrices = skuReductionTo.getMemberPrice();
            // 过滤掉会员价格不大于0的
            List<MemberPriceEntity> memberPriceEntities = memberPrices.stream()
                    .map(memberPrice -> {
                        MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                        memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
                        memberPriceEntity.setMemberLevelId(memberPrice.getId());
                        memberPriceEntity.setMemberLevelName(memberPrice.getName());
                        memberPriceEntity.setMemberPrice(memberPrice.getPrice());
                        memberPriceEntity.setAddOther(1);
                        return memberPriceEntity;
                    })
                    .filter(memberPrice -> memberPrice.getMemberPrice().compareTo(BigDecimal.ZERO) > 0)
                    .collect(Collectors.toList());
            memberPriceEntityList.addAll(memberPriceEntities);
        }
        // 保存 sku 阶梯价格
        skuLadderService.saveBatch(skuLadderEntities);
        // 保存 sku 满减
        this.saveBatch(skuFullReductionEntities);
        // 保存 会员价格
        memberPriceService.saveBatch(memberPriceEntityList);
    }

}
